package com.open.launcher.presenter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


import com.open.launcher.base.MyApplication;
import com.open.launcher.bean.allchannels.AllChannel;
import com.open.launcher.bean.allchannels.Channles;
import com.open.launcher.bean.allchannels.Program;
import com.open.launcher.callback.IRequestCallback;
import com.open.launcher.callback.RequestCallback;
import com.open.launcher.service.AHservice;
import com.open.launcher.utils.APPLog;
import com.open.launcher.utils.Consts;
import com.open.launcher.utils.Paths;
import com.open.library.utils.PreferencesUtils;

import java.util.List;

import static android.content.ContentValues.TAG;


/**
 * Created by Shinelon on 2017/9/15.
 */

public class AllChannelsPresenter {
    private boolean isDisplayed;
    private Callback callback;
    public List<AllChannel.DataBean> listContent;
    private List<Channles.LivesBean> programList;
//    private List<Program.Epg> epgList;
    private AllChannelsViewHolder viewHolder;
    private Program currentProgram;
    private int programId;
    private Activity mActivity;
    private AHservice service;
    public AllChannelsPresenter(Activity mActivity) {
        this.mActivity = mActivity;
    }

    public void setDisplayed(boolean displayed) {
        isDisplayed = displayed;
    }

    //加载数据
    public void loadItem(Bundle params, final Callback callback) {
        this.callback = callback;
        service = new AHservice();
        String url = params.getString("url");
        APPLog.printInfo("开始加载频道列表数据");
        service.channelAllCallback(url, new RequestCallback() {
            @Override
            public void onLoadComplete(String contentID, int pageNumber, int count, List contents) {
                if (callback == null || !isDisplayed) {
                    APPLog.printInfo("onLoadComplete:callback :");
                    return;
                }
                if (count > 0) {
                    viewHolder.setLineOneView(true);
                    if (MyApplication.allChannel.getData()!=null){
                    listContent = MyApplication.allChannel.getData();
//                    programList = listContent;
                    }else {
                        listContent = contents;
//                        programList
                    }
                    callback.onMovieCompleted(AllChannelsPresenter.this, count);
                }
            }

            @Override
            public void onLoading(String contentID) {
                Log.i(TAG, "onLoading: ");
            }

            @Override
            public void onFailed(String code, String msg) {
                Log.i(TAG, "onFailed: ");
            }
        });
    }

    public void loadChannels(int positon) {
        int id = listContent.get(positon).getId();
        if (id == MyApplication.COLLECTIION) {
//            List<Program> programs = CacheDataPresenter.getPresenter(mActivity).loadProgramAll();
//            if (callback == null || !isDisplayed) {
//                return;
//            }
//            programList = programs;
//            if (programList.size() != 0) {
//                viewHolder.setLineTwoView(true);
//                callback.onChannelCompleted(AllChannelsPresenter.this, programList.size());
//            }
            viewHolder.setmThreeStageRv(false);
        } else {
            AHservice service = new AHservice();
//             +programId
            service.channelDetailCallback("http://"+ PreferencesUtils.getString(mActivity, Consts.IP_ADDR_KEY)+":"+PreferencesUtils.getString(mActivity, Consts.IP_ADDR_PORT)+ Paths.CHANNEL_LIST_PATH+ id, new RequestCallback() {
                @Override
                public void onLoadComplete(String contentID, int pageNumber, int count, List contents) {
                    if (callback == null || !isDisplayed) {
                        APPLog.printInfo("onLoadComplete:callback :");
                        return;
                    }
                    programList = contents;
                    Log.i(TAG, "onLoadComplete: "+count);
                    if (programList.size() != 0) {
                        viewHolder.setLineTwoView(true);
                        callback.onChannelCompleted(AllChannelsPresenter.this, count);
                    }
                    viewHolder.setmThreeStageRv(false);
                }

                @Override
                public void onLoading(String contentID) {
                    Log.i(TAG, "onLoading: ");
                }

                @Override
                public void onFailed(String code, String msg) {
                    Log.i(TAG, "onFailed: ");
                }
            });
        }
    }

    public Channles.LivesBean toPlayPosition(int position) {
        if (programList != null && position <= programList.size()) {
            Channles.LivesBean program = programList.get(position);
            return program;
            //if (program != null) {
            //    if (clickPosition != null) {
            //        clickPosition.callbackPosition(program);
            //    } else {
            //        Intent intent = new Intent();
            //        intent.putExtra("type", 1002);
            //        intent.putExtra("data", program);
            //        intent.setClass(mActivity, PlayerActivity.class);
            //        mActivity.startActivity(intent);
            //        mActivity.finish();
            //    }
            //}
        }
        return null;
    }

    public Bundle toPlayTvodProgram(int position) {
        Bundle bundle = new Bundle();
//        String url = currentProgram.getReview();
////        url = "http://58.241.83.166:8081/0068/index.m3u8";
//        String startTime = epgList.get(position).getStarttime();
//        String endTime = epgList.get(position).getEndtime();
//        Program.Epg eng = epgList.get(position);
//        //http://192.168.0.39:8081/0001/index.m3u8?begin=0&end=1499003542
//        String reviewUrl = url + "?begin=" + startTime + "&end=" + endTime;
//        bundle.putString("startTime",startTime);
//        bundle.putString("endTime",endTime);
//        bundle.putString("reviewUrl",reviewUrl);
//        bundle.putSerializable("program",eng);
//        bundle.putInt("type",1003);
        return bundle;
//        String url = currentProgram.getReview();
////        url = "http://58.241.83.166:8081/0068/index.m3u8";
//        String startTime = epgList.get(position).getStarttime();
//        String endTime = epgList.get(position).getEndtime();
//        Program.Epg eng = epgList.get(position);
//        //http://192.168.0.39:8081/0001/index.m3u8?begin=0&end=1499003542
//        String reviewUrl = url + "?begin=" + startTime + "&end=" + endTime;
//        APPLog.printInfo("reviewUrl==>:" + reviewUrl);
//        if (url == null) {
//            Toast.makeText(mActivity, "暂无回看内容", Toast.LENGTH_SHORT).show();
//            return;
//        }
        //if (clickPosition != null) {
        //    clickPosition.lookBackCallback(url, programId, epgList, position);
        //}
//        Intent intent = new Intent();
//        intent.putExtra("type", 1003);
//        intent.putExtra("data", eng);
//        intent.putExtra("playUrl", reviewUrl);
//        intent.setClass(mActivity, PlayerActivity.class);
//        mActivity.startActivity(intent);
    }

    public void loadProgram(int position) {
        //epgList = programList.get(position).getEpg();
        programId = programList.get(position).getId();
        //currentProgram = programList.get(position);
        //if (epgList != null) {
        //    callback.onProgramCompleted(AllChannelsPresenter.this, epgList.size());
        //}
        //viewHolder.setmThreeStageRv(true);

//        service = new AHservice();
//        service.loadVodItemData("http://"+PreferencesUtils.getString(mActivity, Consts.IP_ADDR_KEY)+":"+PreferencesUtils.getString(mActivity, Consts.IP_ADDR_PORT)+ Paths.LIVE_LIST_PATH +programId,new IRequestCallback(){
//
//            @Override
//            public void onLoadComplete(String contentID, int pageNumber, int count, Object dto) {
////                if (dto!=null) {
////                    currentProgram = (Program) dto;
////                    epgList = ((Program) dto).getEpg();
////                }
////                if (epgList != null) {
////                        callback.onProgramCompleted(AllChannelsPresenter.this, epgList.size());
////                    }
//                viewHolder.setmThreeStageRv(true);
//            }
//
//            @Override
//            public void onLoading(String contentID) {
//
//            }
//
//            @Override
//            public void onFailed(String code, String msg) {
//
//            }
//        });
    }

    public void onBindMainChanelView(MainViewHolder holder, int position) {
        if (!isDisplayed) {
            return;
        }
        holder.setmainChanelName(listContent.get(position).getName());
    }

    public void onBindChannel(SencondViewHolder viewHolder, int position) {
        if (isDisplayed) {
//            String number = String.valueOf(programList.get(position).getNumber());
//            String name = programList.get(position).getName();
//            String wholeName="";
//            if (number.length()==1){
//                wholeName=number+"  "+name;
//            }
//            else if (number.length()==2){
//                wholeName=number+" "+name;
//            }
//            Log.i("knightqi", "onBindChannel: "+wholeName);
            viewHolder.setChannel(programList.get(position).getNumber()+"/"+programList.get(position).getName());
        }
    }

    public void onBindProgram(ProgramViewHolder viewHolder, int position) {
        if (!isDisplayed) {
            return;
        }
//        long time = System.currentTimeMillis();
//        viewHolder.setTitleText(epgList.get(position).getTitle());
//        viewHolder.setStartTimeText(SystemTimeDate.stampToMMddDate(epgList.get(position).getStarttime()));
//        viewHolder.setEndTimeText(SystemTimeDate.stampToHHmmDate(epgList.get(position).getStarttime()) + " - " + SystemTimeDate.stampToHHmmDate(epgList.get(position).getEndtime()));
//        long startLt = new Long(epgList.get(position).getStarttime());
//        long endLt = new Long(epgList.get(position).getEndtime());
//        startLt *= 1000;
//        endLt *= 1000;
//        if (time >= startLt && time <= endLt) {
//            viewHolder.setTypeBtnText("直播", 1);
//        } else if (time > endLt) {
//            viewHolder.setTypeBtnText("回看", 0);
//        } else if (time < startLt) {
//            viewHolder.setTypeBtnText("预约", 0);
//        }
    }

    public void onBindAllChannels(AllChannelsViewHolder viewHolder) {
        this.viewHolder = viewHolder;

    }

    public interface Callback {
        void onError(int code, String msg);

        void onMovieCompleted(AllChannelsPresenter presenter, int count);

        void onChannelCompleted(AllChannelsPresenter presenter, int count);

        void onProgramCompleted(AllChannelsPresenter presenter, int count);
    }

    public interface MainViewHolder {
        void setmainChanelName(String name);
    }

    public interface SencondViewHolder {
        void setChannel(String name);
    }

    public interface ProgramViewHolder {
        void setTitleText(String name);

        void setTypeBtnText(String name, int type);

        void setStartTimeText(String name);

        void setEndTimeText(String name);

    }

    public interface AllChannelsViewHolder {
        void setmThreeStageRv(boolean isShow);

        void setLineTwoView(boolean isShow);

        void setLineOneView(boolean isShow);
    }
}
